from activist.Data import Data
from activist.PersonInformation import PersonInformation
from activist.MetaData import MetaData, MetaDataFactory
from activist.Users import Users
from activist import errors



def commandCreate(name,phone,email,address):
    return Person(name,phone,email,address)


class Person(Data):
    def __init__(self,expectedid=None):
        super(Person,self).__init__(expectedid)
        self.command("create")
        self.command("edit")
        self.command("make_administrator")
        self.command("make_moderator")
        self.command("make_commentator")
        self.command("send_message")
        
    command_create_VERSION = 0
    def command_create(self,name,username,passwd,phone,email,address,bio,private):
        self._createcalled = True
        self._username = username
        Users.addUser(username,passwd,self.id())
        PersonInformation( self, name, phone, email, address, bio, private )

    def name(self):
        return self.info().name()
    def username(self):
        return self._username
    def phone(self):
        return self.info().phone()
    def email(self):
        return self.info().email()
    def address(self):
        return self.info().address()
    def private(self):
        return self.info().private()

    def isAdministrator(self):
        from activist.DataStore import DataStore
        for x in DataStore['administrator'].metadata():
            if x.fromData() is self:
                return True
        return False

    def isModerator(self):
        from activist.DataStore import DataStore
        for x in DataStore['moderator'].metadata():
            if x.fromData() is self:
                return True
        return False

    def isCommentator(self):
        from activist.DataStore import DataStore
        for x in DataStore['commentator'].metadata():
            if x.fromData() is self:
                return True
        return False

    def groups(self):
        from activist.Group import Group
        rtn = []
        for x in self.sourcesForRelationship(MetaData.BelongsWith):
            if isinstance(x,Group):
                rtn.append(x)
        return rtn
        

    def getXML(self):
        rtn = """<person class="activist.Person" id="%(id)s">
%(meta)s
</person>""" % { "id":self.id(),
                 "meta":self.getMetaXML()
                 }
        return rtn
    
    command_make_administrator_VERSION = 0
    def command_make_administrator(self,person,val):
        from activist.DataStore import DataStore
        if val:
            meta = MetaDataFactory(self,MetaData.BelongsWith,DataStore['administrator'])
            MetaDataFactory(meta,MetaData.Creator,person)
            return meta

        rtn = None
        for x in DataStore['administrator'].metadata():
            if x.fromData() is self:
                rtn = x.delete(person)
        return rtn
            
    command_make_moderator_VERSION = 0
    def command_make_moderator(self,person,val):
        from activist.DataStore import DataStore
        if val:
            meta = MetaDataFactory(self,MetaData.BelongsWith,DataStore['moderator'])
            MetaDataFactory(meta,MetaData.Creator,person)
            return meta

        for x in DataStore['moderator'].metadata():
            if x.fromData() is self:
                return x.delete(person)
            
    command_make_commentator_VERSION = 0
    def command_make_commentator(self,person,val):
        from activist.DataStore import DataStore
        if val:
            meta = MetaDataFactory(self,MetaData.BelongsWith,DataStore['commentator'])
            MetaDataFactory(meta,MetaData.Creator,person)
            return meta

        for x in DataStore['commentator'].metadata():
            if x.fromData() is self:
                return x.delete(person)


    command_edit_VERSION = 0
    def command_edit(self,editedBy,name,phone,email,address,bio,private):
        rtn = PersonInformation( self, name, phone, email, address, bio, private )
        meta = MetaDataFactory(rtn,MetaData.InformationOf,self)
        if editedBy:
            MetaDataFactory(meta,MetaData.Creator,editedBy)
        return rtn


    command_send_message_VERSION = 0
    def command_send_message(self,message):
        MetaDataFactory(self,MetaData.Message,message)
        
        

    def info(self):
        meta = self.metadataInverseTargetsForRelationship(MetaData.InformationOf)
        return meta[0].fromData()

    def __str__(self):
        return '<Person %s>' % self.id()
    def __repr__(self):
        return '<Person %s>' % self.id()
